package com.brillilab.common.utils;

import java.util.Random;
import java.util.UUID;

import com.brillilab.common.constant.ClientTypeEnum;

/**
 * Code生成的工具类
 *
 * @author wmh
 */
public class CodeUtil {

	//去掉了1,0,i,o几个容易混淆的字符  
    public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";  

    /** 
     * 使用系统默认字符源生成验证码 
     * @param size    验证码长度
     * @return 
     */  
    public static String getCode(int size){  
        return getCode(size, VERIFY_CODES);  
    }  
    
    /** 
     * 使用指定源生成验证码 
     * @param size    验证码长度
     * @param sources   验证码字符源 
     * @return 
     */  
    public static String getCode(int size, String sources){  
        if(sources == null || sources.length() == 0){  
            sources = VERIFY_CODES;  
        }  
        int codesLen = sources.length();  
        Random rand = new Random();
        StringBuilder verifyCode = new StringBuilder(size);  
        for(int i = 0; i < size; i++){  
            verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));  
        }  
        return verifyCode.toString();  
    }

    /**
     * 获取随机数
     */
    public static String getRandom(int length){
        StringBuilder sb=new StringBuilder();

        Random random=new Random();
        for (int i=0;i<length;i++){
            sb.append(random.nextInt(10));
        }

        return sb.toString();
    }

    /**
     * 生成uuid
     */
    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","").toLowerCase();
    }

    /**
     * 生成Token
     */
    public static String getToken(ClientTypeEnum clientType){
        return clientType.getValue()+"_"+getUUID();
    }
}
